package com.data.center.controller;

import com.data.center.dto.OrganizationTagDTO;
import com.data.center.entity.OrganizationTag;
import com.data.center.exception.ResultBody;
import com.data.center.mapper.OrganizationTagMapper;
import com.data.center.service.OrganizationTagService;
import com.data.center.vo.OrganizationTagVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Li_Jx
 * @description: 组织标签表
 * @date 2023/11/8 18:25
 */
@RestController
@RequestMapping("/organizationTag")
@Slf4j
public class OrganizationTagController {
    @Autowired
    private OrganizationTagService organizationTagService;
    @Autowired
    private OrganizationTagMapper organizationTagMapper;
    /**
     * @description 一个通用的查询接口
     * @author Li_Jx
     * @Date 2023/11/9 19:41
     */
    @GetMapping("/select")
    public ResultBody select(OrganizationTagDTO organizationTagDTO){
        OrganizationTag organizationTagFiler = new OrganizationTag();
        org.springframework.beans.BeanUtils.copyProperties(organizationTagDTO,organizationTagFiler);
        List<OrganizationTag> organizationTagList = organizationTagService.select(organizationTagFiler);
        return ResultBody.success(organizationTagList);
    }

    /**
     * @description 一个通用的通过id更新接口
     * @author Li_Jx
     * @Date 2023/11/9 19:41
     */
    @PutMapping("/update")
    public ResultBody update(OrganizationTagDTO organizationTagDTO){
        String id = organizationTagDTO.getId();
        if(id == null || id.isEmpty()){
            return ResultBody.error("标签名称为空");
        }
        List<OrganizationTag> organizationTagList = organizationTagMapper.findById(id);
        if (organizationTagList.size() != 1){
            return ResultBody.error("此id无对应数据");
        }
        OrganizationTag organizationTag = new OrganizationTag();
        org.springframework.beans.BeanUtils.copyProperties(organizationTagDTO,organizationTag);
        organizationTagService.update(organizationTag);
        return ResultBody.success();
    }

    /**
     * @description 通过标签名称获取标签列表
     * @author Li_Jx
     * @Date 2023/11/9 16:33
     */
    @GetMapping("/findOrgTag")
    public ResultBody findOrgTag(String name){
        if(name == null || name.isEmpty()){
            return ResultBody.error("标签名称为空");
        }
        List<OrganizationTag> organizationTagList = organizationTagService.findOrgTag(name);
        if(organizationTagList.isEmpty()){
            return ResultBody.error("未查询到匹配结果");
        }
        return ResultBody.success(organizationTagList.get(0));
    }

    /**
     * @description 新增标签，前提：1.此标签名称不存在 2.此标签名称存在
     * @author Li_Jx
     * @Date 2023/11/9 16:46
     */
    @PostMapping("/addOrgTag")
    public ResultBody addOrgTag(OrganizationTagDTO organizationTagDTO){
        OrganizationTag organizationTag = new OrganizationTag();
        org.springframework.beans.BeanUtils.copyProperties(organizationTagDTO,organizationTag);
        String name = organizationTag.getName();
        if(name == null || name.isEmpty()){
            return ResultBody.error("标签名称为空，无法新增");
        }
        // TODO 标签名称的规范性验证
        List<OrganizationTag> organizationTagList = organizationTagService.findOrgTag(name);
        if(!organizationTagList.isEmpty()){
            return ResultBody.error("标签名称已存在，无法新增");
        }
        organizationTagService.addOrgTag(organizationTag);
        return ResultBody.success();
    }

    /**
     * @description 修改标签名称，前提：1.曾用名存在且唯一 2.曾用名和新名均不为空 3.新名在数据库中无同名数据
     * @Param OrganizationOrgTagDTO
     * @author Li_Jx
     * @Date 2023/11/9 17:09
     */
    @PutMapping("/renameOrgTag")
    public ResultBody renameOrgTag(OrganizationTagDTO organizationTagDTO){
        List<OrganizationTag> organizationTagList = organizationTagMapper.findById(organizationTagDTO.getId());
        if (organizationTagList.size() != 1){
            return ResultBody.error("未找到符合修改条件的标签");
        }
        String name = organizationTagDTO.getName();
        if(name == null || name.isEmpty()){
            return ResultBody.error("新标签名称为空，无法更新");
        }
        List<OrganizationTag> organizationTagList2 = organizationTagService.findOrgTag(name);
        if (organizationTagList2.size() != 0){
            return ResultBody.error("新标签名称不可用");
        }
        OrganizationTag organizationTag = new OrganizationTag();
        organizationTag.setName(name);
        organizationTag.setId(organizationTagDTO.getId());
        // TODO 标签名称的规范性验证
        organizationTagService.renameOrgTag(organizationTag);
        return ResultBody.success();
    }
    /**
     * @description 通过id删除标签
     * @author Li_Jx
     * @Date 2023/11/21 18:49
     */
    @DeleteMapping("/deleteOrgTag")
    public ResultBody deleteOrgTag(OrganizationTagDTO organizationTagDTO){
        String id = organizationTagDTO.getId();
        if(id == null || id.isEmpty()){
            return ResultBody.error("标签id为空，无法删除");
        }
        List<OrganizationTag> organizationTagList = organizationTagMapper.findById(id);
        if (organizationTagList.size() != 1){
            return ResultBody.error("未找到符合删除条件的标签");
        }
        organizationTagService.deleteByName(organizationTagList.get(0).getName());
        return ResultBody.success();
    }
    /**
     * @description 融合标签：将savedId关联外键中的id改为sourceOrgTag的id，并对deletedId进行物理删除。
     * @param savedId(融合标签),deletedId(被融合标签)
     * @author Li_Jx
     * @Date 2023/11/10 16:59
     */
    @PutMapping("/mergeOrgTag")
    public ResultBody mergeOrgTag(@RequestParam("savedId")String savedId, @RequestParam("deletedId")String deletedId){
        if (savedId == null || savedId.isEmpty() ||
                deletedId == null || deletedId.isEmpty()){
            return  ResultBody.error("输入数据id为空");
        }
        OrganizationTag sourceOrgTagFilter = new OrganizationTag();
        sourceOrgTagFilter.setId(savedId);
        OrganizationTag targetOrgTagFilter = new OrganizationTag();
        targetOrgTagFilter.setId(deletedId);
        if (organizationTagService.select(sourceOrgTagFilter).isEmpty() || organizationTagService.select(targetOrgTagFilter).isEmpty()){
            return ResultBody.error("需要融合的标签不存在");
        }
        organizationTagService.mergeOrgTag(sourceOrgTagFilter,targetOrgTagFilter);
        return ResultBody.success();
    }

}
